package conf

import (
	"fmt"
	"os"
	"reflect"

	"github.com/akkuman/parseConfig"
)

func MapToStruct(data map[string]interface{}, s interface{}) error {
	t := reflect.ValueOf(s).Elem()
	for k, v := range data {
		val := t.FieldByName(k)
		value := reflect.ValueOf(v)

		val.Set(value)
	}
	return nil
}

func PathExists(path string) bool {
	_, err := os.Stat(path)
	if err == nil {
		return true
	}
	if os.IsNotExist(err) {
		return false
	}
	return false
}

func GetConfig(str, filename string) interface{} {
	config := parseConfig.New(filename)

	defer func() {
		if err := recover(); err != nil {
			fmt.Printf("%s -- %s ", err, "file.go读取配置")
		}
	}()
	/**
	获取到一个map
	*/
	return config.Get(str)
}
